﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Mrc.Config
{
    public class KVConfigStoreCenter : JsonConfigurationFileParser
    {
        private Dictionary<string, string> _kv { get; set; }= new Dictionary<string,string>(StringComparer.OrdinalIgnoreCase);   

        public KVConfigStoreCenter(string jsonStr)
        {
            Add(StreamHelpers.StringToStream(jsonStr));
        }
        public KVConfigStoreCenter(List<string> jsonList)
        {
            foreach (var item in jsonList)
            {
                Add(StreamHelpers.StringToStream(item));                
            }
        }
        public KVConfigStoreCenter(Stream  stream)
        {
            Add(stream);
        }
        public KVConfigStoreCenter(List<Stream> streams)
        {             
            foreach (var stream in streams)
            {
                Add(stream);
            }
        }
        public void Add(Stream stream)
        {
            foreach (var tempKV in ParseStream(stream))
            {
                if (!_kv.ContainsKey(tempKV.Key))
                    _kv.Add(tempKV.Key, tempKV.Value);
            }
        }      
        public Dictionary<string, string> AllKV { get { return _kv; } }

        public List<string> Keys { get { return _kv.Keys.ToList(); } }

        public string Get(string name)
        {
            if (!_kv.ContainsKey(name))
            {
                throw new ArgumentNullException("no key");
            }
            return _kv[name];
        }
    }
}
